package com.mytijian.code.facade;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mytijian.code.dto.MashangBaseResult;
import com.mytijian.code.param.InvalidParam;
import com.mytijian.code.param.ModifyMobileParam;
import com.mytijian.code.param.ModifyValidityParam;
import com.mytijian.code.param.ReSendParam;
import com.mytijian.code.param.RefundSuccessParam;
import com.mytijian.code.param.SendParam;
import com.mytijian.code.param.SuitSuccessParam;
import com.mytijian.code.service.InvalidCodeService;
import com.mytijian.code.service.OrderUserInfoService;
import com.mytijian.code.service.SendCodeService;
import com.mytijian.runtime.model.param.AliCodeMerchantCommonParam;
import com.mytijian.runtime.model.service.AliService;
import com.mytijian.runtime.model.service.ServiceFacade;
import com.mytijian.utils.BeanUtil;

/**
 * 阿里码商门面类
 * Created by shjh on 2017/9/19.
 */
@Service
public class AliCodeMerchantServiceFacade implements ServiceFacade {

	private Logger logger = LoggerFactory.getLogger(AliCodeMerchantServiceFacade.class);

	@Autowired
	private SendCodeService sendCodeService;
	@Autowired
	private InvalidCodeService invalidCodeService;
	@Autowired
	private OrderUserInfoService orderUserInfoService;

	/**
	 * 接收发码通知
	 *
	 * @param aliCodeMerchantCommonParam
	 */
	@AliService(aliMethodName = "send")
	public MashangBaseResult acceptSend(AliCodeMerchantCommonParam aliCodeMerchantCommonParam) {
		SendParam param = BeanUtil.createObject(aliCodeMerchantCommonParam, SendParam.class);
		sendCodeService.acceptSend(param);
		return getSuccessMsg();
	}

	/**
	 * 接收重新发码通知
	 *
	 * @param aliCodeMerchantCommonParam
	 */
	@AliService(aliMethodName = "resend")
	public MashangBaseResult acceptResend(AliCodeMerchantCommonParam aliCodeMerchantCommonParam) {
		ReSendParam param = BeanUtil.createObject(aliCodeMerchantCommonParam, ReSendParam.class);
		sendCodeService.acceptResend(param);
		return getSuccessMsg();
	}

	/**
	 * 通知码商修改手机号
	 *
	 * @param aliCodeMerchantCommonParam
	 */
	@AliService(aliMethodName = "modifyMobile")
	public MashangBaseResult acceptModifyMobile(AliCodeMerchantCommonParam aliCodeMerchantCommonParam) {
		ModifyMobileParam param = BeanUtil.createObject(aliCodeMerchantCommonParam, ModifyMobileParam.class);
		orderUserInfoService.acceptModifyMobile(param);
		return getSuccessMsg();
	}

	/**
	 * 通知码商修改有效期
	 *
	 * @param aliCodeMerchantCommonParam
	 */
	@AliService(aliMethodName = "modifyValidity")
	public MashangBaseResult acceptModifyValidity(AliCodeMerchantCommonParam aliCodeMerchantCommonParam) {
		ModifyValidityParam param = BeanUtil.createObject(aliCodeMerchantCommonParam, ModifyValidityParam.class);
		sendCodeService.acceptModifyValidity(param);
		return getSuccessMsg();
	}

	/**
	 * 通知码商退款成功
	 *
	 * @param aliCodeMerchantCommonParam
	 */
	@AliService(aliMethodName = "refundSuccess")
	public MashangBaseResult acceptRefundSuccess(AliCodeMerchantCommonParam aliCodeMerchantCommonParam) {
		RefundSuccessParam param = BeanUtil.createObject(aliCodeMerchantCommonParam, RefundSuccessParam.class);
		invalidCodeService.acceptRefundSuccess(param);
		return getSuccessMsg();
	}

	/**
	 * 通知码商维权成功
	 */
	@AliService(aliMethodName = "suitSuccess")
	public MashangBaseResult acceptSuitSuccess(AliCodeMerchantCommonParam aliCodeMerchantCommonParam) {
		SuitSuccessParam param = BeanUtil.createObject(aliCodeMerchantCommonParam, SuitSuccessParam.class);
		invalidCodeService.acceptSuitSuccess(param);
		return getSuccessMsg();
	}

	/**
	 * 通知码商订单作废成功
	 */
	@AliService(aliMethodName = "invalid")
	public MashangBaseResult acceptInvalid(AliCodeMerchantCommonParam aliCodeMerchantCommonParam) {
		InvalidParam param = BeanUtil.createObject(aliCodeMerchantCommonParam, InvalidParam.class);
		invalidCodeService.acceptInvalid(param);
		return getSuccessMsg();
	}

	/**
	 * 心跳连接通知
	 * @param aliCodeMerchantCommonParam
	 * @return
	 */
	@AliService(aliMethodName = "heartBeatCheck")
	public MashangBaseResult acceptHeartBeatCheck(AliCodeMerchantCommonParam aliCodeMerchantCommonParam) {
		logger.info("心跳连接通知param: " + JSONObject.toJSONString(aliCodeMerchantCommonParam));
		return getSuccessMsg();
	}

	private MashangBaseResult getSuccessMsg() {
		MashangBaseResult result = new MashangBaseResult();
		result.setCode("isv.success-all");
		result.setMsg("成功");
		return result;
	}

}
